<?php

	session_start();

	include_once('../classes/Database.php');
	include_once('../classes/Aerodrome.php');
	include_once('../classes/Runway.php');
	include_once('../classes/Frequency.php');
	include_once('../classes/Search.php');

	$Database = new Database();
	$Database->init();
	$handler = $Database->handler;

	//if (!isset($_SESSION['token']) || !isset($_POST['request']) || $_POST['request']['token'] != $_SESSION['token']) {

	//	header('Location: ../index.php');

	//} else {

		// parse the search string
	    $_POST['request']['keywords'] = $handler->real_escape_string(strtoupper($_POST['request']['keywords']));
	    $aerodromes = Search::find($_POST['request']['keywords'], $handler);
	    $aerodromes = array_reverse($aerodromes);

	//}

	$token = md5(uniqid(rand(), true));
	$_SESSION['token'] = $token;

?>

<!DOCTYPE html>

<!--[if IEMobile 7 ]><html class="MIEM7 no-js" lang="en" ><![endif]-->
<!--[if IEMobile 8 ]><html class="MIEM8 no-js" lang="en" ><![endif]-->
<!--[if IEMobile 9 ]><html class="MIEM9 no-js" lang="en" ><![endif]-->
<!--[if !(IEMobile) ]><!--><html class="no-js" lang="en" ><!--<![endif]-->

<head>

	<!-- METAS -->
	<meta charset="utf-8" />

	<title>mobile aerodrome information service</title>

	<meta name="author" content="mobile aerodrome information service" />
	<meta name="description" content="mobile aerodrome information service" />

	<!-- METAS -->
	<meta itemprop="name" content="mobile aerodrome information service" />
	<meta itemprop="description" content="mobile aerodrome information service" />
	<meta itemprop="image" content="" />

	<meta property="og:type" content="website" />
	<meta property="og:url" content="http://wx.devenv.us" />
	<meta property="og:site_name" content="mobile aerodrome information service" />

	<meta property="og:title" content="mobile aerodrome information service" />
	<meta property="og:description" content="mobile aerodrome information service" />

	<meta property="og:image" content="" />
	<meta property="og:image:secure_url" content="" />
	<meta property="og:locale" content="en_US" />

	<!-- PHONE -->
	<meta name="mobileoptimized" content="320" />
	<meta name="handheldfriendly" content="true" />
	<meta name="viewport" content="
				width=device-width,
				minimum-scale=1.0,
				maximum-scale=1.0,
				initial-scale=1.0,
				user-scalable=no"
				/>

	<!-- APPLE -->
	<meta name="apple-mobile-web-app-capable" content="yes" />
	<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
	<meta name="format-detection" content="telephone=yes" />

	<!-- MSIES -->
	<meta http-equiv="cleartype" content="on" />

	<!-- FAVOR -->
	<link rel="apple-touch-startup-image" type="image/png" href="apple.start.image.png?v0.800" />
	<link rel="apple-touch-icon-precomposed" type="image/png" href="apple.touch.icon.a.png?v0.800" />
	<link rel="apple-touch-icon-precomposed" type="image/png" href="apple.touch.icon.d.png?v0.800" sizes="144x144" />
	<link rel="apple-touch-icon-precomposed" type="image/png" href="apple.touch.icon.c.png?v0.800" sizes="114x114" />
	<link rel="apple-touch-icon-precomposed" type="image/png" href="apple.touch.icon.b.png?v0.800" sizes="72x72" />
	<link rel="apple-touch-icon-precomposed" type="image/png" href="apple.touch.icon.a.png?v0.800" sizes="57x57" />


	<!-- JSONS -->
	<script type="text/javascript" src="../js/xui.js"></script>
	<script type="text/javascript" src="../js/modernizr-2.0.6.min.js"></script>
	<script type="text/javascript" src="../js/geo.js"></script>
	<script type="text/javascript" src="../js/edge.js"></script>
	<script type="text/javascript" src="../js/helper.js"></script>
	<script type="text/javascript" src="../js/persistent.js"></script>
	<!--<script type="text/javascript" src="js/downstream.js"></script>-->
	<script type="text/javascript" src="../js/bookmark_bubble.js"></script>
	<script type="text/javascript" src="../js/json2.js"></script>
	<script type="text/javascript" src="../js/script.js"></script>

	<!-- MAPS -->
	<script type="text/javascript" src="http://www.openlayers.org/api/OpenLayers.js"></script>
	<script type="text/javascript" src="../js/openMaps.js"></script>

	<!-- FONTS -->
    <link rel="stylesheet" type="text/css" href="../css/fontello.css"><!--[if IE 7]>
    <link rel="stylesheet" type="text/css" href="../css/fontello-ie7.css"><![endif]-->
	<script src="http://use.edgefonts.net/marvel.js"></script>
	<script src="http://use.edgefonts.net/share-regular.js"></script>

	<!-- STYLES -->
	<link rel="stylesheet" type="text/css" href="../css/styles.css" />

</head>

<body class="aerodromes">

	<section role="search">
	  <form method="post">
	    <input name="request[keywords]" type="search" value="search aerodromes" autocorrect="off" autocapitalize="off">
	    <input name="request[token]" type="hidden" value="<?php echo $token;?>">
	  </form>
	</section>

	<header id="header">
	  <h1><?php echo (count($aerodromes) > 1) ? $_POST['request']['keywords'] : $aerodromes[0]->title; ?></h1>
	  <a href="#" id="search_trigger"><i class='icon-search-1'></i></a>
	  <div class="sticky" data-role="header">
	  	<i class="filter_icon"></i>
	  </div>
	</header>

	<section role="main">
	  <div id="aerodrome_img">
	    <div class="container">
	      <p id="breadcrumbs"><a href="/"><i class="icon-reply"></i> main page</a></p>

	      <section id="closest" class="clearfix">

	        <?php 

	        	//var_dump($aerodromes);
	        	foreach ($aerodromes as $key => $aerodrome) {
	        	?>
			        <span class="resultset <?php echo $aerodrome->code; ?>">

			        	<!-- GENERAL INFO & MAPS -->
						<article class="aerodrome_info">
							
			        		<!-- GENERAL INFO -->
							<div class="aerodrome_basic">
								<div class="aerodrome_name">
									<?php echo $aerodrome->title; ?>
								</div>
								<div class="aerodrome_ident">
									<?php echo $aerodrome->code; ?>
								</div>

								<div class="aerodrome_extras">
									<span>
										<p class="time_stamp label">ELEVATION</p>
										<p class="location value"><strong><?php echo $aerodrome->location['elevation'] . ' ft'; ?></strong></p>
									</span>
									<span>
										<p class="time_stamp label">LATITUDE</p>
										<p class="location value"><strong><?php echo $aerodrome->location['lat']; ?></strong></p>
									</span>
									<span>
										<p class="time_stamp label">LONGITUDE</p>
										<p class="location value"><strong><?php echo $aerodrome->location['lon']; ?></strong></p>
									</span>
									<span>
										<p class="time_stamp label">COUNTRY</p>
										<p class="location value"><strong><?php echo $aerodrome->country; ?></strong></p>
									</span>									
								</div>

								<div class="aerodrome_basic_weather">
									<span>
										<p class="time_stamp label">WIND</p>
										<p class="location value latest_conditions_wind"></p>
									</span>
									<span>
										<p class="time_stamp label">VISIBILITY</p>
										<p class="location value latest_conditions_visibility"></p>
									</span>
									<span>
										<p class="time_stamp label">TEMPERATURE</p>
										<p class="location value latest_conditions_temperature"></p>
									</span>									
									<span>
										<p class="time_stamp label">PRESSURE</p>
										<p class="location value latest_conditions_pressure"></p>
									</span>

								</div>

							</div>

							<!-- Toggles -->
							<div class="main_info_toggles">
								<span class="map_toggle active_toggle">
									<i class="icon-globe"></i>
									<p>map</p>
								</span>
								<span class="runway_toggle">
									<i class="icon-road"></i>
									<p>runways</p>
								</span>
								<span class="comm_toggle">
									<i class="icon-signal"></i>
									<p>communication</p>
								</span>
							</div>

							<div class="weather_toggles">
								<span class="map_toggle active_toggle">
									<i class="icon-globe"></i>
									<p>visual wx</p>
								</span>								
								<span class="metar_toggle">
									<i class="icon-cloud-1"></i>
									<p>metars</p>
								</span>
								<span class="taf_toggle">
									<i class="icon-cloud"></i>
									<p>tafs</p>
								</span>
							</div>





							<div class="toggle_container">

								<!-- OPEN MAPS -->
								<div class="openMap toggle"
				        			 id='<?php echo $aerodrome->code; ?>' 
				        			 lat='<?php echo $aerodrome->location['lat']; ?>' 
				        			 lon='<?php echo $aerodrome->location['lon']; ?>'
				        			 long='<?php echo $aerodrome->getLongestRunway(); ?>'>
				        		</div>	

								<!-- COMM -->
								<div class="aerodrome_comm hide toggle">

									<p>* Verify before use: may be inaccurate or out of date.</p>
									<?php

										$longest_runway = 0;
										foreach ($aerodrome->frequencies as $key => $value) {?>

											<div class="comm_container">

												<span class="frequency">
													<p><?php echo $value->type . ':'; ?>
													<strong><?php echo $value->frequency_mhz; ?></strong></p>
												</span>
												<span class="description">
													<p>(<?php echo $value->description; ?>)</p>
												</span>
											</div>


										<?}?>
								</div>

								<!-- METARS -->
								<div class="aerodrome_metars hide toggle"></div>

								<!-- TAFS -->
								<div class="aerodrome_tafs hide toggle"></div>

								<!-- RUNWAY INFO -->
								<div class="aerodrome_runways hide toggle">
									<p>* click the runway for details</p>
									<?php
										$longest_runway = 0;
										foreach ($aerodrome->runways as $key => $value) {
											$hasMoreInfo = false;
											$runway_length = (int) $value->length_ft;
											if ($longest_runway == 0) {
												$container_width = 100;
												$longest_runway = $runway_length;
											} else {
												$container_width = round($runway_length*100/$longest_runway);
											}
											if ( strlen($value->le_displaced_threshold_ft) > 0 ||
												strlen($value->he_displaced_threshold_ft) > 0 ||
												strlen($value->le_elevation_ft) > 0 ||
												strlen($value->he_elevation_ft) > 0 ||
												strlen($value->le_heading_degT) > 0 ||
												strlen($value->he_heading_degT) > 0 ) {
												$hasMoreInfo = true;
											}
											?>
											<div class="runway_container">
												<div class="runway" style="width:<?php echo $container_width;?>%;">
													<?php if (strlen($value->le_ident) > 0) {?>
														<span class="le_ident"><?php echo $value->le_ident;?></span>
													<?}?>
													<span class="runway_length"><?php echo $runway_length;?> ft <?php echo (($hasMoreInfo) ? "*" : "");?></span>
													<?php if (strlen($value->he_ident) > 0) {?>
														<span class="he_ident"><?php echo $value->he_ident;?></span>
													<?}?>
												</div>
												<div class="runway_more_info">
													
													<!-- DISPLACED THRESHOLD -->
													<?php if (strlen($value->le_displaced_threshold_ft) > 0 || strlen($value->he_displaced_threshold_ft) > 0) {?>
														<div class="info_section">
															<?php if (strlen($value->le_displaced_threshold_ft) > 0) {?>
																<span class="le_displaced_threshold_ft"><span class="markings"><i class="icon-right-open"></i><i class="icon-right-open"></i><i class="icon-right-open"></i><i class="icon-right-open"></i>&nbsp;</span><?php echo $value->le_displaced_threshold_ft;?>&nbsp;ft</span>
															<?}?>
															<span class="displaced_threshold_ft">displaced threshold</span>
															<?php if (strlen($value->he_displaced_threshold_ft) > 0) {?>
																<span class="he_displaced_threshold_ft"><?php echo $value->he_displaced_threshold_ft;?>&nbsp;ft&nbsp;<span class="markings"><i class="icon-left-open"></i><i class="icon-left-open"></i><i class="icon-left-open"></i><i class="icon-left-open"></i></span></span>
															<?}?>
														</div>
													<?}?>

													<!-- ELEVETION AT TOUCHDOWN -->
													<?php if (strlen($value->le_elevation_ft) > 0 || strlen($value->he_elevation_ft) > 0) {?>
														<div class="info_section">
															<?php if (strlen($value->le_elevation_ft) > 0) {?>
																<span class="le_elevation_ft"><?php echo $value->le_elevation_ft;?>&nbsp;ft</span>
															<?}?>
															<span class="elevation_ft">TDP elevation</span>
															<?php if (strlen($value->he_elevation_ft) > 0) {?>
																<span class="he_elevation_ft"><?php echo $value->he_elevation_ft;?>&nbsp;ft</span>
															<?}?>
														</div>
													<?}?>

													<!-- TRUE HEADING -->
													<?php if (strlen($value->le_heading_degT) > 0 || strlen($value->he_heading_degT) > 0) {?>
														<div class="info_section">
															<?php if (strlen($value->le_heading_degT) > 0) {?>
																<span class="le_heading_degT"><?php echo $value->le_heading_degT;?>&#176;</span>
															<?}?>
															<span class="heading_degT">true heading</span>
															<?php if (strlen($value->he_heading_degT) > 0) {?>
																<span class="he_heading_degT"><?php echo $value->he_heading_degT;?>&#176;</span>
															<?}?>
														</div>
													<?}?>
												</div>
											</div>
										<?}?>
								</div>

							</div>


		

						</article>

					</span>


	        	<?
	        	
	        	}

	        ?>


	      </section>
	      <a name="bottom"></a>
	      <!--<div class="full" id="more_controls">
	      	<a href="#" id="less">- less</a>
	        <a href="#" id="more">+ more results</a>
	      </div>-->
	    </div>
	  </div>
	</section>


	<!--<section id="location_wrap">
	  <div class="container">
	    <h1>location</h1>
	    <p id="location">Alamance, NC</p>
	    <a href="/locations" class="button">change your location</a>
			<p><a id="use_current_link" href="/locations/gps/">use your current location</a></p>
		  </div>
	</section>-->

	<footer role="contentinfo" class="clearfix">
	  <div class="container">
	    <p id="footer_social">
      		<a href="http://facebook.com/"><i class="icon-facebook"></i></a>&nbsp;
      		<a href="http://twitter.com/"><i class="icon-twitter"></i></a>
	    </p>
	    <ul>
	      <li><a href="/fedback">feedback</a></li>
	      <li><a href="/events">social</a></li>
	      <li><a href="/favorites">credits</a></li>

	    </ul>
	    <a href="#" id="full_site">Settings</a>
	    <p><small>Disclaimer : Pilots are advised to obtain an interpretative weather briefing and all pertinent NOTAM from Flight Information Centres / Flight Service Stations (FIC/FSS) prior to flying. The user accepts sole responsibility and all risks associated with use of the information on this web site. The user expressly releases THIS JOINT, its employees, agents and assigns from any liability or obligation in respect thereof.</small></p>
	  </div>
	</footer>

	<script type="text/javascript">
		// iOS scale bug fix
		MBP.scaleFix();
		MBP.hideUrlBar();

		// Hide the address bar!
		(function() {
			setTimeout(function(){
				window.scrollTo(0, 1);
			}, 0);
		});

		x$.ready(function () {

		// Init js framework
			wxMobile.initialize({
				"denied":0,
				"unavailable":0,
				"lat":"36.0329",
				"lng":"-79.4872",
				"city_id":"1851",
				"city":"Alamance",
				"county_id":"181",
				"area_id":"150",
				"user":1,
				"modified":1347143272,
				"distance":null
			});


		// Save the search query for next time
			wxMobile.saveSearch("<?php echo $_POST['request']['keywords'];?>");


		// ADDS wx request
			wxMobile.requestADDS("<?php echo $_POST['request']['keywords'];?>", 'metars');
			wxMobile.requestADDS("<?php echo $_POST['request']['keywords'];?>", 'tafs');


		// Generate open maps
		 	x$(".openMap").each(function () {
		 		generateOpenMap(x$(this).attr('id')[0], x$(this).attr("lat")[0], x$(this).attr("lon")[0], x$(this).attr("long")[0]);
		 	});



		});

	</script>
</body>
</html>